package com.jinzhi.jzweb.controller.backstage;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.JazSysContractDO;
import com.jinzhi.jzweb.domain.JazSysProjectDO;
import com.jinzhi.jzweb.domain.JazUserAuntDO;
import com.jinzhi.jzweb.domain.JazUserOrderDO;
import com.jinzhi.jzweb.domain.param.ContractQuery;
import com.jinzhi.jzweb.domain.vo.JazUserContractVO;
import com.jinzhi.jzweb.domain.vo.JazUserOrderVO;
import com.jinzhi.jzweb.domain.vo.userAunt.UserVO;
import com.jinzhi.jzweb.service.JazSysContractService;
import com.jinzhi.jzweb.service.JazSysProjectService;
import com.jinzhi.jzweb.service.JazUserOrderService;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/*
 *  @author : JingChangTian
 *  @date   : 2023/7/11 15:06
 *  @description :
 *
 */
@Controller
@RequestMapping("/sysContract")
@Api(value = "合同后台管理", tags = "合同后台管理")
public class SysContractController {

    private final JazSysContractService jazSysContractService;

    private final JazUserOrderService jazUserOrderService;

    private final JazSysProjectService jazSysProjectService;

    public SysContractController(JazSysContractService jazSysContractService, JazUserOrderService jazUserOrderService, JazSysProjectService jazSysProjectService) {
        this.jazSysContractService = jazSysContractService;
        this.jazUserOrderService = jazUserOrderService;
        this.jazSysProjectService = jazSysProjectService;
    }


    @ApiOperation(value = "分页查询合同列表", notes = "分页查询合同列表")
    @ApiImplicitParams({

    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping (value = "/getSysContractPage")
    public Result<Page<?>> getSysContractPage(ContractQuery contractQuery){
        return Result.ok(jazSysContractService.getSysContractPage(contractQuery));
    }

    @Log(value = "新增合同",sysModule = 7,operationType = 1)
    @ApiOperation(value = "新增合同", notes = "新增合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "JazSysContractDO", value = "JazSysContractDO", required = true, dataType = "JazSysContractDO"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping (value = "/insertSysContract")
    public Result<String> insertSysContract(@RequestBody JazUserOrderDO jazUserOrderDO){
        Boolean aBoolean = jazSysContractService.insertSysContract(jazUserOrderDO);
        return aBoolean ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "新增合同--发起方", notes = "新增合同--发起方")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "", value = "", required = true, dataType = ""),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping (value = "/initiator")
    public Result<List<JazUserOrderVO>> initiator(){
        return Result.ok(jazSysContractService.initiator());
    }

    @ApiOperation(value = "新增合同--签约方", notes = "新增合同--签约方")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "", value = "", required = true, dataType = ""),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping (value = "/signed")
    public Result<List<JazUserOrderVO>> signed(){
        return Result.ok(jazSysContractService.signed());
    }

    @Log(value = "修改合同启用状态",sysModule = 7,operationType = 3)
    @ApiOperation(value = "修改启用状态", notes = "修改启用状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "JazSysContractDO", value = "JazSysContractDO", required = true, dataType = "JazSysContractDO"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/updateContractStatus")
    public Result updateContractStatus(@RequestBody ContractQuery contractQuery){
        return jazSysContractService.updateContractStatus(contractQuery);
    }

    @Log(value = "修改合同",sysModule = 7,operationType = 3)
    @ApiOperation(value = "修改合同", notes = "修改合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "JazSysContractDO", value = "JazSysContractDO", required = true, dataType = "JazSysContractDO"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping (value = "/updateSysContract")
    public Result<String> updateSysContract(@RequestBody JazSysContractDO jazSysContractDO){
        jazSysContractDO.setUpdateTime(new Date());
        Boolean aBoolean = jazSysContractService.updateById(jazSysContractDO);
        return aBoolean ? Result.ok() : Result.fail();
    }

    @Log(value = "删除合同",sysModule = 7,operationType = 2)
    @ApiOperation(value = "根据id删除合同", notes = "根据id删除合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping (value = "/deletedSysContract")
    public Result<String> deletedSysContract(@RequestParam("id") Long id){
        // 查询合同信息
        JazUserOrderDO jazUserOrderDO = jazUserOrderService.selectById(id);
        if (jazUserOrderDO == null){
            return Result.fail();
        }
        // 获取发起签约人id
        Long sysUserid = jazUserOrderDO.getInitiateId();
        Long signedUserId = jazUserOrderDO.getSigningId();
        if (sysUserid == null && signedUserId == null){
            return Result.fail();
        }
        //验证有没有正在进行中的订单
        List<JazUserOrderDO> jazUserOrderDOs = jazUserOrderService.selectList(new EntityWrapper<JazUserOrderDO>().
                eq("initiate_id",sysUserid).eq("signing_id",signedUserId).
                notIn("status",2,3));
        if(!jazUserOrderDOs.isEmpty()){
            return Result.fail("有正在进行的订单无法删除");
        }
        Boolean aBoolean = jazUserOrderService.deleteById(id);
        return aBoolean ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "根据id查询合同详情", notes = "根据id查询合同详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping (value = "/getSysContractById")
    public Result<?> getSysContractById(@RequestParam("id") Long id){
        return Result.ok(jazSysContractService.getSysContractById(id));
    }

    @ApiOperation(value = "合同详情页面入口", notes = "合同详情页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/info")
    public String info(){
        return "jzweb/jazSysContract/info";
    }
}
